package com.leo.dao;

import javax.annotation.Resource;

import com.leo.entity.Score;
import com.leo.entity.StuUid;
import com.leo.entity.Student;
import com.leo.entity.User;

public class InterfaceDaoImp implements InterfaceDao  {
	@Resource
	private Dao dao;

	@Override
	public Student findStudent(String uid,String devName){
		String hql = "select uid from StuUid uid inner join fetch uid.user u where uid.oid=? and u.username=?";
		return ( (StuUid) dao.excuteQuery(hql, uid, devName).get(0) ).getStudent();
	}
	
	@Override
	public Student findStudent(String stuID){
		return  dao.find(Student.class, "stuID=?", stuID);
	}

	@Override
	public void updateOrSaveScore(Score score){
		dao.updateOrSave(score);	
	}

	@Override
	public void updateOrSaveStudent(Student student){
		dao.updateOrSave(student);	
	}

	@Override
	public String findUid(String stuID, String devName){
		String hql = "select u from User u "
				+ "inner join u.stuUids uid "
				+ "inner join uid.student s where u.username=? and s.stuID=?";
		String uid = ((User) dao.excuteQuery(hql,devName, stuID)
				.get(0)).getStuUids().get(0).getOid();
		return uid;
	}

	@Override
	public User findUser(String username){
		return dao.find(User.class, "username=?", username);
	}

	@Override
	public void addUid(StuUid uid){
		dao.save(uid);
	}
}
